package com.itheima.web.service.impl.sms;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.exception.CustomException;
import com.itheima.service.sms.SmsFlashPromotionSessionService;
import com.itheima.vo.sms.SmsFlashPromotionProductRelation;
import com.itheima.vo.sms.SmsFlashPromotionSession;
import com.itheima.web.mapper.sms.SmsFlashPromotionSessionMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.stream.Collectors;

@DubboService
public class SmsFlashPromotionSessionServiceImpl extends ServiceImpl<SmsFlashPromotionSessionMapper, SmsFlashPromotionSession> implements SmsFlashPromotionSessionService {

    @Autowired
    private FlashPromotionProductRelationServiceImpl flashPromotionProductRelationService;

    /**
     * 添加场次
     *
     * @param smsFlashPromotionSession
     * @return
     */
    @Override
    public int create(SmsFlashPromotionSession smsFlashPromotionSession) {
        if (smsFlashPromotionSession == null) {
            throw new CustomException("暂无数据");
        }
        boolean result = this.save(smsFlashPromotionSession);
        return result ? 1 : 0;
    }

    /**
     * 删除场次
     *
     * @param id
     * @return
     */
    @Override
    public int delete(Long id) {
        boolean result = this.removeById(id);
        return result ? 1 : 0;
    }

    /**
     * 获取全部场次
     *
     * @return
     */
    @Override
    public List<SmsFlashPromotionSession> selectAll() {
        return this.list();
    }

    /**
     * 获取全部可选场次及其数量
     *
     * @param flashPromotionId
     * @return
     */
    @Override
    public List<SmsFlashPromotionSession> selectList(Long flashPromotionId) {
//        //根据关联表查询FlashPromotionSessionId；
//        QueryWrapper<SmsFlashPromotionProductRelation> lqw = new QueryWrapper<>();
//        //LambdaQueryWrapper<SmsFlashPromotionProductRelation> lqw =new LambdaQueryWrapper<>();
//        //lqw.eq(flashPromotionId != null, SmsFlashPromotionProductRelation::getFlashPromotionId, flashPromotionId);
//        //QueryWrapper的lambda用法去重
//        lqw.select("DISTINCT `flash_promotion_session_id`").lambda().eq(flashPromotionId != null,SmsFlashPromotionProductRelation::getFlashPromotionId,flashPromotionId);
//        List<SmsFlashPromotionProductRelation> relationList = flashPromotionProductRelationService.list(lqw);
//        //遍历集合
//        List<SmsFlashPromotionSession> list = relationList.stream().map((flashPromotionProductRelation) -> {
//            Long flashPromotionSessionId = flashPromotionProductRelation.getFlashPromotionSessionId();
//            LambdaQueryWrapper<SmsFlashPromotionSession> wrapper = new LambdaQueryWrapper<>();
//            wrapper.eq(SmsFlashPromotionSession::getStatus, 1);
//            wrapper.eq(flashPromotionSessionId != null, SmsFlashPromotionSession::getId, flashPromotionSessionId);
//            return this.getOne(wrapper);
//        }).collect(Collectors.toList());
        LambdaQueryWrapper<SmsFlashPromotionSession> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(SmsFlashPromotionSession::getStatus,1);
        List<SmsFlashPromotionSession> list = this.list(wrapper);


        return list;
    }

    /**
     * 修改启用状态
     *
     * @param id
     * @param smsFlashPromotionSession
     * @return
     */
    @Override
    public int updateStatus(Long id, SmsFlashPromotionSession smsFlashPromotionSession) {
        //构造条件构造器
        LambdaUpdateWrapper<SmsFlashPromotionSession> luw = new LambdaUpdateWrapper<>();
        luw.eq(id != null, SmsFlashPromotionSession::getId, id);
        luw.set(smsFlashPromotionSession.getStatus() != null, SmsFlashPromotionSession::getStatus, smsFlashPromotionSession.getStatus());
        boolean result = this.update(luw);
        return result ? 1 : 0;
    }

    /**
     * 修改场次
     *
     * @param id
     * @param smsFlashPromotionSession
     * @return
     */
    @Override
    public int updateFlashPromotion(Long id, SmsFlashPromotionSession smsFlashPromotionSession) {
        LambdaQueryWrapper<SmsFlashPromotionSession> lqw = new LambdaQueryWrapper<>();
        lqw.eq(id != null, SmsFlashPromotionSession::getId, id);
        boolean result = this.update(smsFlashPromotionSession, lqw);
        return result ? 1 : 0;
    }

    /**
     * 获取场次详情
     *
     * @param id
     * @return
     */
    @Override
    public SmsFlashPromotionSession selectById(Long id) {
        return this.getById(id);
    }
}
